<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input :placeholder="'请输入' + tempDesc.tplName" v-model="listQuery.tplName" style="width: 200px;margin-right: 20px" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleCreate">{{ $t('table.add') }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">

      <el-table-column :label="tempDesc.tplName" width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.tplName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.content">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="tempDesc.isRelatedSubscribeText" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isRelatedSubscribeText == 1 ? 'success' : 'danger'" hit>{{ scope.row.isRelatedSubscribeText == 1 ? '&nbsp;&nbsp;是&nbsp;&nbsp;' : '&nbsp;&nbsp;否&nbsp;&nbsp;' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.isRelatedReceiveText" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isRelatedReceiveText == 1 ? 'success' : 'danger'" hit>{{ scope.row.isRelatedReceiveText == 1 ? '&nbsp;&nbsp;是&nbsp;&nbsp;' : '&nbsp;&nbsp;否&nbsp;&nbsp;' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.isRelatedMenu" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isRelatedMenu == 1 ? 'success' : 'danger'" hit>{{ scope.row.isRelatedMenu == 1 ? '&nbsp;&nbsp;是&nbsp;&nbsp;' : '&nbsp;&nbsp;否&nbsp;&nbsp;' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column :label="tempDesc.createTime" width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.updateTime" width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px"
               style="width: 400px; margin-left:50px;">
        <el-form-item :label="tempDesc.tplName" prop="tplName">
          <el-input v-model="temp.tplName" :placeholder="'请输入' + tempDesc.tplName"/>
        </el-form-item>
        <el-form-item :label="tempDesc.content" prop="account">
          <el-input v-model="temp.content" :placeholder="'请输入' + tempDesc.content"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">{{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {fetchList, createRow, updateRow, deleteRow} from '@/api/wx-text-template'
  import waves from '@/directive/waves' // Waves directive
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  //常量title
  const tempDesc = {
    "tplName": "模板名称",
    "content": "模板内容",
    "isRelatedSubscribeText":"是否已关联欢迎语",
    "isRelatedReceiveText":"是否已关联关键字",
    "isRelatedMenu":"是否已关联菜单",
    "createTime": "创建时间",
    "updateTime": "更新时间"
  };
  export default {
    //name: 'ComplexTable',
    components: {Pagination},
    directives: {waves},
    filters: {},
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          tplName: ""
        },
        showReviewer: false,
        temp: {
          "id": "",
          "tplName": "",
          "content": "",
          "createTime": "",
          "updateTime": ""
        },
        tempDesc: tempDesc,
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '增加'
        },
        rules: {
          tplName: [{required: true, message: '请输入' + tempDesc.tplName, trigger: 'blur'}],
          content: [{required: true, message: '请输入' + tempDesc.content, trigger: 'blur'}]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.list
          this.total = response.data.total

          // Just to simulate the time of the request
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetTemp() {
        this.temp = {
          "id": "",
          "tplName": "",
          "content": "",
          "createTime": "",
          "updateTime": ""
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createRow(this.temp).then(() => {
              this.getList();
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            updateRow(tempData).then(() => {
              this.getList();
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleDelete(row) {
        if(row.isRelatedSubscribeText == '1' || row.isRelatedReceiveText == '1' || row.isRelatedMenu == '1'){
          this.$message({ message: '请先解除关联', type: 'warning' });
          return false;
        }
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var params = {"id": row.id};
            deleteRow(params).then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              this.getList();
            });
          });
      }
    }
  }
</script>
